package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_business.ADInterstitialManager
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_implement.LoadFacebookAd
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_implement.LoadGoogleAd
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ad_implement.LoadIronSourceAd
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.system.exitProcess


class BackActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.back_activity)
        closeAppToAdStatus()
    }

    /**
     * 根据广告展示状态，控制程序退出
     */
    private fun closeAppToAdStatus() {
        val flag = ADInterstitialManager.mInstance.showInterstitialAD(this, "Interstitial_Back")
        if (flag) {
            LoadGoogleAd.mInstance.setOnAdCloseListener(object : LoadGoogleAd.AdClose {
                override fun onClose() {
                    myFinish()
                }

                override fun onOpen() {
                }
            })
            LoadFacebookAd.mInstance.setCloseListener(object : LoadFacebookAd.OnCloseListener {
                override fun onClose() {
                    myFinish()
                }
            })
            LoadIronSourceAd.mInstance.setOnAdCloseListener(object : LoadIronSourceAd.AdClose {
                override fun onClose() {
                    myFinish()
                }

                override fun onOpen() {
                }
            })
//            LoadUnityAd.mInstance.setOnAdCloseListener(object : LoadUnityAd.AdClose {
//                override fun onClose() {
//                    myFinish()
//                }
//
//                override fun onOpen() {
//                }
//            })
        } else {
            myFinish()
        }
    }

    // 延迟0.5秒退出
    private fun myFinish() {
        GlobalScope.launch(Dispatchers.Main) {
            delay(500L)
            finish()
            exitProcess(0)
        }
    }

}